package com.twu.biblioteca;


import java.io.*;
import java.util.Scanner;


public class BibliotecaApp {

    public static void main(String[] args) {


        System.out.println("Welcome!\n");
        Library lib = new Library();
        lib.addBooks("Books.txt");
        lib.addMovies("Movies.txt");
        while(true)
        {
            System.out.println("\nSelect 1 of the following options:");
            System.out.println("1 - View all books");
            System.out.println("2 - View all movies");
            System.out.println("3 - Log in/Log out");
            System.out.println("4 - Reserve Book");
            System.out.println("5 - Check membership details");
            System.out.println("0 - Exit\n");
            Scanner inputScanner = new Scanner(System.in);
            String input = inputScanner.nextLine();
            if(!checkValidity(input)){
                System.out.println("Select valid option\n");
                continue;
            }
            handleInput(lib,Integer.parseInt(input));
        }
    }

    private static void handleInput(Library lib,int option) {
        if(option == 1){
            lib.displayAllBooks();
        }
        else if(option == 2){
            lib.displayAllMovies();
        }
        else if(option == 3){
            User currentUser = lib.getCurrentUser();
            if(currentUser != null){
                lib.logout();
                System.out.println(currentUser.getName() + " logged out\n");
            }
            else
                //login(lib);
                lib.login();
        }
        else if(option == 4){
            User currentUser = lib.getCurrentUser();
            if(currentUser == null){
                System.out.println("\nYou must be logged in to reserve a book");
                return;
            }
            lib.displayAvailableBooks();
            System.out.println("\nEnter the id of the book to be reserved");
            Scanner inputScanner = new Scanner(System.in);
            String input = inputScanner.nextLine();
            int id;
            try{
                id = Integer.parseInt(input);
            }
            catch(Exception e){
                System.out.println("Invalid id\n");
                return;
            }
            if(lib.reserveBook(id) == true){
                System.out.println("Thank You! Enjoy the book\n");
            }
            else
                System.out.println("Sorry we don't have that book yet\n");
        }
        else if(option == 5){
            User currentUser = lib.getCurrentUser();
            if(currentUser != null){
                System.out.println("Membership details");
                System.out.println("Name : " + currentUser.getName());
                System.out.println("Email Id : " + currentUser.getEmailId());
                System.out.println("Phone Number : " + currentUser.getPhoneNo());
            }
            else
                System.out.println("Please talk to a Librarian. Thank you.\n");
        }
        else if(option == 0){
            System.out.println("Thank you\n");
            lib.writeToFile();
            System.exit(0);
        }
    }


    private static boolean checkValidity(String input) {
        int optionSelected;
        try{
            optionSelected = Integer.parseInt(input);
        }
        catch (Exception e){
            return false;
        }

        if(optionSelected < 0 || optionSelected > 5){
            return false;
        }
        else{
            return true;
        }
    }
}
